package com.rabc.framework.web.domain;

import com.rabc.web.entity.SysUser;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;

@Data
public class LoginUserDetails implements UserDetails {


    private Integer id;

    private String username;

    private String password;


    private SysUser sysUser;
    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 部门ID
     */
    private Long deptId;

    /**
     * 用户唯一标识
     */
    private String token;

    /**
     * 登录时间
     */
    private Long loginTime;

    /**
     * 过期时间
     */
    private Long expireTime;

    /**
     * 登录IP地址
     */
    private String ipaddr;

    /**
     * 登录地点
     */
    private String loginLocation;

    /**
     * 浏览器类型
     */
    private String browser;

    /**
     * 操作系统
     */
    private String os;

    /**
     * 权限列表
     */
    private Set<String> permissions;

    public LoginUserDetails(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public LoginUserDetails(Integer id, Set<String> permissions, String username, String password, SysUser sysUser) {
        this.id = id;
        this.permissions = permissions;
        this.username = username;
        this.password = password;
        this.sysUser = sysUser;
    }

    public LoginUserDetails(Long userId, Set<String> permissions, String token, String password, String username, SysUser sysUser) {
        this.userId = userId;
        this.permissions = permissions;
        this.token = token;
        this.password = password;
        this.username = username;
        this.sysUser = sysUser;
    }

    public LoginUserDetails(SysUser sysUser) {
        this.sysUser = sysUser;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return Collections.emptyList();
    }

    @Override
    public String getPassword() {
        return sysUser.getPassword();
    }

    @Override
    public String getUsername() {
        return sysUser.getUserName();
    }

    /**
     * 用户账户是否未过期  <p><p/>
     * true，表示用户的账户未过期；false，表示用户的账户已过期。
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 用户的账户是否未被锁定<p></p>
     * true，表示用户的账户未被锁定； false，表示用户的账户已被锁定
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 表示用户的凭据（例如密码）是否未过期
     * true，表示用户的凭据未过期 ;false，表示用户的凭据已过期
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 用于判断用户账户是否启用<p/>
     * true，表示账户处于启用状态；如果返回 false，表示账户处于禁用状态。
     * @return
     */
    @Override
    public boolean isEnabled() {
        return true;
    }
}
